package b2.widget.fort
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.scene.*;
	import b2.scene.fight.*;
	import b2.scene.guide.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.*;
	
	import com.bit101.components.*;
	import com.smartfoxserver.v2.entities.*;
	import com.smartfoxserver.v2.entities.variables.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.geom.Point;

	public class CampDetailTab extends Tab
	{
		public var quickGameButton:BasicButton;
		public var createRoomButton:BasicButton;
		
		private var fortressDialog:FortressDialog;
		private var titleText:TextField;
		private var enemyInfoText:TextField;
		private var goalText:TextField;
		private var modeText:TextField;
		private var lifeText:TextField;
		private var diffText:TextField;
		
		private var modeChooseNob1:RadioButton;
		private var modeChooseNob2:RadioButton;
		
		private var isHeroMode:Boolean;
		
		private var container:Sprite;
		
		public function CampDetailTab(fortressDialog:FortressDialog)
		{
			super("战役详情", 2, 2);
			setPageSelectorPos(200, 400);
			this.fortressDialog = fortressDialog;
			//线框
			var lineRect:Sprite = new Sprite();
			lineRect.graphics.lineStyle(2, 0x27AAE1);
			lineRect.graphics.drawRoundRect(4, 39, 346, 116, 20, 20);
			lineRect.graphics.drawRoundRect(4, 155, 346, 108, 20, 20);
			lineRect.graphics.drawRoundRect(4, 263, 346, 180, 20, 20);
			lineRect.filters = [new DropShadowFilter(2, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			addChild(lineRect);
			//线框色块
			var rrect:Sprite = new Sprite();
			rrect.graphics.beginFill(0x27AAE1);
			rrect.graphics.drawRoundRect(6, 41, 342, 25, 15, 15);
			rrect.graphics.drawRoundRect(6, 157, 342, 25, 15, 15);
			rrect.graphics.drawRoundRect(6, 265, 342, 25, 15, 15);
			rrect.graphics.endFill();
			addChild(rrect);
			//战役名字
			titleText = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.x = 180;
			titleText.y = 38;
			addChild(titleText);
			//胜利条件
			goalText = new TextField();
			goalText.selectable = false;
        	goalText.autoSize = TextFieldAutoSize.LEFT;
			goalText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			goalText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			goalText.x = 15;
			goalText.y = 70;
			addChild(goalText);
			//初始生命
			lifeText = new TextField();
			lifeText.selectable = false;
        	lifeText.autoSize = TextFieldAutoSize.LEFT;
			lifeText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			lifeText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			lifeText.x = 15;
			lifeText.y = 90;
			addChild(lifeText);
			//战役难度
			diffText = new TextField();
			diffText.selectable = false;
        	diffText.autoSize = TextFieldAutoSize.LEFT;
			diffText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			diffText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			diffText.x = 15;
			diffText.y = 110;
			addChild(diffText);
			//战役模式
			modeText = new TextField();
			modeText.selectable = false;
        	modeText.autoSize = TextFieldAutoSize.LEFT;
			modeText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			modeText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			modeText.htmlText = "<font color='#FFFF00'>战役模式</font>      普通      <font color='#FF4444'>英雄</font>";
			modeText.x = 15;
			modeText.y = 130;
			addChild(modeText);
			Style.BUTTON_FACE = 0x043950;
			Style.embedFonts = false;
			Style.fontName = "微软雅黑";
			Style.fontSize = 12;
			modeChooseNob1 = new RadioButton(this, 75, 136, "      ", true, onNormalMode);
			modeChooseNob1.addEventListener(MouseEvent.MOUSE_OVER, onModeMouseOver);
			modeChooseNob1.addEventListener(MouseEvent.MOUSE_OUT, onModeMouseOut);
			modeChooseNob2 = new RadioButton(this, 123, 136, "      ", false, onHeroMode);
			modeChooseNob2.addEventListener(MouseEvent.MOUSE_OVER, onModeMouseOver);
			modeChooseNob2.addEventListener(MouseEvent.MOUSE_OUT, onModeMouseOut);
			//掉落物品
			var dropItemText:TextField = new TextField();
			dropItemText.selectable = false;
        	dropItemText.autoSize = TextFieldAutoSize.LEFT;
			dropItemText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFF00);
			dropItemText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			dropItemText.text = "战场掉落物品";
			dropItemText.x = 180;
			dropItemText.y = 70;
			addChild(dropItemText);
			//敌方信息
			enemyInfoText = new TextField();
			enemyInfoText.selectable = false;
        	enemyInfoText.autoSize = TextFieldAutoSize.LEFT;
			enemyInfoText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			enemyInfoText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			enemyInfoText.x = 150;
			enemyInfoText.y = 155;
			addChild(enemyInfoText);
			//房间信息
			var roomInfoText:TextField = new TextField();
			roomInfoText.selectable = false;
        	roomInfoText.autoSize = TextFieldAutoSize.CENTER;
			roomInfoText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFF00);
			roomInfoText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			roomInfoText.text = "房间信息";
			roomInfoText.x = 150;
			roomInfoText.y = 265;
			addChild(roomInfoText);
			//显示空房间
			for (var i:int = 0; i < 4; i++)
			{
				var roomItem:RoomItem = new RoomItem();
				roomItem.x = 15 + (i % 2) * (roomItem.width + 5);
				roomItem.y = 297 + Math.floor(i / 2) * (roomItem.height + 5);
				addChild(roomItem);
			}
			//快速开始
			var ButtonBgr0:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			quickGameButton = new BasicButton(ButtonBgr0, "快速开始", 12, 70, 25);
			quickGameButton.x = 20;
			quickGameButton.y = 410;
			quickGameButton.addEventListener(MouseEvent.CLICK, onQuickGameButtonClick);
			addChild(quickGameButton);
			//创建房间
			createRoomButton = new BasicButton(ButtonBgr0, "创建房间", 12, 70, 25);
			createRoomButton.x = 105;
			createRoomButton.y = 410;
			createRoomButton.addEventListener(MouseEvent.CLICK, onCreateRoomButtonClick);
			addChild(createRoomButton);
			setPagePos(15, 297);
			setPageSelectorPos(210, 412);
		}
		
		public function refresh():void
		{
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			//文字信息
			var campConfig:CampConfig = Config.campConfigs[campIndex];
			var battleConfig:BattleConfig = Config.battleConfigs[campConfig.battleIndex[0]];
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortressDialog.fortIndex];
			//背景水纹
			var enemies:Array = campConfig.getEnemy();
			var enemyConfig:EnemyConfig = Config.enemyConfigs[enemies[0][0]];
			var tankConfig:TankConfig = Config.objectConfigs[enemyConfig.tankIndex];
			var Bgr:Class = ResourceHelper.getInstance().getClassByName(tankConfig.iconImage);
			var bgr:Sprite = new Bgr();
			bgr.x = 200;
			bgr.y = 150;
			bgr.width = 350;
			bgr.height = 350;
			if (!isHeroMode)
				bgr.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
			bgr.alpha = 0.2;
			fortressDialog.changeBgr(bgr);
			titleText.text = campConfig.name;
			var lifeNum:int = campConfig.lifeNum;
			if (isHeroMode)
				lifeNum = 1;
			goalText.htmlText = "<font color='#FFFF00'>胜利条件</font>   " + Config.basicConfig.goalName[battleConfig.goal];
			if (campConfig.type == 1)
			{
				modeText.visible = false;
				modeChooseNob1.visible = false;
				modeChooseNob2.visible = false;
			}
			else
			{
				modeText.visible = true;
				modeChooseNob1.visible = true;
				modeChooseNob2.visible = true;
			}
			lifeText.htmlText = "<font color='#FFFF00'>初始生命</font>   " + lifeNum + "条";
			if (container != null && container.parent != null)
			{
				container.parent.removeChild(container);
			}
			container = new Sprite();
			//奖励信息
			for (var i:int = 0; i < Math.min(campConfig.awards.length, 3); i++)
			{
				var award:CampAwardConfig = campConfig.awards[i];
				var objectConfig:CommonObjConfig = Config.objectConfigs[award.objectIndex];
				var basicLevel:int = award.basicLevel;
				var strengthenLevel:int = award.strengthenLevel;
				//英雄模式奖励
				if (isHeroMode)
				{
					if (!objectConfig.noBasicLevel)
					{
						if (!objectConfig.noStrengthenLevel)
						{
							strengthenLevel = (award.strengthenLevel + fortressConfig.heroMode.equipLevelIncrease) % 5;
							basicLevel = (award.basicLevel + (award.strengthenLevel + fortressConfig.heroMode.equipLevelIncrease) / 5) % 5;
						}
						else
						{
							basicLevel = (award.basicLevel + fortressConfig.heroMode.medalLevelIncrease) % 5;
						}
					}
				}
				var objectItem:ObjectItem = new ObjectItem(award.objectIndex, basicLevel, strengthenLevel, 0, award.objectNum, -1, false);
				objectItem.x = 180 + i * 55;
				objectItem.y = 95;
				container.addChild(objectItem);
			}
			//比赛战役
			if (campConfig.type == 1)
			{
				enemyInfoText.text = "随机坦克";
				diffText.htmlText = "<font color='#FFFF00'>人数设定</font>   " + campConfig.recommendedPlayer + "v" + campConfig.recommendedPlayer;
			}
			//普通战役
			else
			{
				enemyInfoText.text = "敌方信息";
				if (isHeroMode)
				{
					//英雄模式难度提高
					campConfig.difficulty += fortressConfig.heroMode.enemyLevelIncrease;
					diffText.htmlText = "<font color='#FFFF00'>战役难度</font>   " + campConfig.getDiffStar();
					campConfig.difficulty -= fortressConfig.heroMode.enemyLevelIncrease;
				}	
				else
					diffText.htmlText = "<font color='#FFFF00'>战役难度</font>   " + campConfig.getDiffStar();
			}
			//敌人信息
			for (i = 0; i < Math.min(enemies.length, 4); i++)
			{
				var enemyItem:EnemyTankItem;
				var enemyLevel:int = isHeroMode ? enemies[i][1] + fortressConfig.heroMode.enemyLevelIncrease : enemies[i][1];
				var enemyBossLevel:int = isHeroMode && enemies[i][2] > 0 ? enemies[i][2] + fortressConfig.heroMode.enemyBossLevelIncrease : enemies[i][2]; 
				if (isHeroMode)
					enemyItem = new EnemyTankItem(enemies[i][0], enemyLevel, enemyBossLevel);
				else
					enemyItem = new EnemyTankItem(enemies[i][0], enemyLevel, enemyBossLevel);
				enemyItem.x = 18 + i * 82;
				enemyItem.y = 187;
				container.addChild(enemyItem);
			}
			addChild(container);
			//房间
			this.removeAll();
			var roomList:Array = SmartFoxHelper.getInstance().sfs.roomManager.getRoomList();
			for (i = 0; i < roomList.length; i++)
			{
				var room:Room = roomList[i];
				var roomCampIndex:int = parseInt(room.name.substr(room.name.length - 3));
				if (room.isGame && roomCampIndex == campIndex && room.name.charAt(0) != "0")
				{
					var user:User = room.userList[0];
					if (user != null)
					{
						var playerId:int = user.getVariable("playerId").getIntValue();
						var playerName:String = user.getVariable("playerName").getStringValue();
						var tankId:int = user.getVariable("tankId").getIntValue();
						var tankIndex:int = user.getVariable("tankIndex").getIntValue();
					}
					var roomItem:RoomItem = new RoomItem(room);
					this.addItem(roomItem, roomItem.width, roomItem.height);
					if (this.items.length >= 12)
						break;
				}
			}
			createRoomButton.disable = false;
			if (isHeroMode)
				createRoomButton.disable = true;
		}
		
		private function onQuickGameButtonClick(event:MouseEvent):void
		{
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			var campConfig:CampConfig = Config.campConfigs[campIndex];
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortressDialog.fortIndex];
			//比赛战役
			if (campConfig.type == 1)
			{
				onCreateRoomButtonClick(event);
			}
			//普通战役
			else
			{
				if (isHeroMode)
				{
					var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
					var num:int = me.getObjNum(CommonObjConfig.HERO_VERT, 0, 0);
					if (num < fortressConfig.heroMode.heroVertCostNum)
					{
						var popMessage:PopMessage = new PopMessage("英雄之证不足", 0xFF0000);
						WidgetManager.getInstance().popOut([popMessage], false, true);
					}
					else
					{
						var messageBox:MessageBox = new MessageBox("需要消耗<font color='#00AA00'> " + fortressConfig.heroMode.heroVertCostNum + 
							" </font>个英雄之证，是否继续？", true);
						messageBox.buttonOK.addEventListener(MouseEvent.CLICK, onStartSingleGame);
						WidgetManager.getInstance().popOut([messageBox], false, true);
					}
				}
				else
				{
					GuideScene.getInstance().clearLastStep();
					onStartSingleGame(null);
				}
			}
		}
		
		private function onStartSingleGame(event:MouseEvent):void
		{
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortressDialog.fortIndex];
			if (isHeroMode)
				ServerHelper.getInstance().expendVert(0, 11, fortressConfig.heroMode.heroVertCostNum, 0 ,0);
			SceneManager.getInstance().loadScene(FightScene.getInstance());
			FightScene.getInstance().loadCamp(campIndex, isHeroMode);
		}
		
		private function onCreateRoomButtonClick(event:MouseEvent):void
		{
			if (createRoomButton.disable)
				return;
			createRoomButton.disable = true;
			var campIndex:int = fortressDialog.currentSelectedCampItem.campIndex;
			SmartFoxHelper.getInstance().createRoom(campIndex);
		}
		
		private function onModeMouseOver(event:MouseEvent):void
		{
			var itemTip:BasicMessage;
			var globalPoint:Point;
			var fortressConfig:FortressConfig = Config.fortressConfigs[fortressDialog.fortIndex];
			if (event.currentTarget == modeChooseNob1)
			{
				globalPoint = modeChooseNob1.parent.localToGlobal(new Point(modeChooseNob1.x, modeChooseNob1.y));
				itemTip = new BasicMessage("难度低，掉落品质低");
			}
			else
			{
				globalPoint = modeChooseNob2.parent.localToGlobal(new Point(modeChooseNob2.x, modeChooseNob2.y));
				itemTip = new BasicMessage("难度高，掉落品质高，消耗<font color='#00AA00'> " + 
					fortressConfig.heroMode.heroVertCostNum + " </font>个英雄之证");
			}
			itemTip.x = globalPoint.x;
			itemTip.y = globalPoint.y + 18;
			WidgetManager.getInstance().popOutMouseOverMessage(itemTip);
		}
		
		private function onModeMouseOut(event:MouseEvent):void
		{
			WidgetManager.getInstance().closeMouseOverMessage();
		}
		
		private function onNormalMode(event:Event):void
		{
			if (!isHeroMode)
			{
				return;
			}
			isHeroMode = false;
			//转换按钮
			modeChooseNob2.selected = false;
			refresh();
		}
		
		private function onHeroMode(event:Event):void
		{
			if (isHeroMode)
			{
				return;
			}
			isHeroMode = true;
			//转换按钮
			modeChooseNob1.selected = false;
			refresh();
		}
	}
}